home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / iqb9109.zip / CVTREAL.BAS < prev    next >
BASIC Source File  |  1991-09-09  |  3KB  |  124 lines

  1. ' CvtReal.Bas
  2. ' Program to convert IEEE floating point numbers to MBF and backwards again
  3. '
  4.  
  5. CONST FALSE = 0, TRUE = NOT FALSE
  6.  
  7. TYPE SingleMBFStr
  8.     Bytes   AS STRING * 4
  9. END TYPE
  10. TYPE TrickSingle
  11.     Bytes   AS SINGLE
  12. END TYPE
  13. TYPE DoubleMBFStr
  14.     Bytes   AS STRING * 8
  15. END TYPE
  16. TYPE TrickDouble
  17.     Bytes   AS DOUBLE
  18. END TYPE
  19.  
  20. DECLARE FUNCTION LZPadStr$ (N$, pLen%)
  21. DECLARE SUB ShowSingle (A AS ANY)
  22. DECLARE SUB ShowDouble (A AS ANY)
  23.  
  24. DIM singleCvBuf AS SingleMBFStr
  25. DIM ieeeSingle AS TrickSingle
  26. DIM mbfSingle AS TrickSingle
  27. DIM doubleCvBuf AS DoubleMBFStr
  28. DIM ieeeDouble AS TrickDouble
  29. DIM mbfDouble AS TrickDouble
  30. DIM Results(20)
  31.  
  32. ' Begin main program
  33.  
  34. ON KEY(1) GOSUB FKey1Handler: KEY(1) ON
  35. ON KEY(2) GOSUB FKey2Handler: KEY(2) ON
  36. ON KEY(3) GOSUB FKey3Handler: KEY(3) ON
  37.  
  38. PRINT "Press F1 to convert a single-precision number,"
  39. PRINT "Press F2 to convert a double-precision number, or"
  40. PRINT "Press F3 to quit."
  41. PRINT
  42.  
  43. Done% = FALSE
  44.  
  45. DO UNTIL Done%
  46.  ' Everything's done in the handlers
  47.  ' This is the future for BASIC main programs!
  48. LOOP
  49. END
  50.  
  51. ' This is the function key handler for converting
  52. ' single-precision real numbers
  53.  
  54. FKey1Handler:
  55.   INPUT "Enter a single-precision floating point number: ", _
  56.          ieeeSingle.Bytes
  57.   singleCvBuf.Bytes = MKSMBF$(ieeeSingle.Bytes)
  58.   LSET mbfSingle = singleCvBuf
  59.   PRINT
  60.   PRINT "The number you typed in IEEE format is ";
  61.   CALL ShowSingle(ieeeSingle)
  62.   PRINT "The number you typed in MBF format is ";
  63.   CALL ShowSingle(mbfSingle)
  64.   X! = mbfSingle.Bytes
  65.   RETURN
  66.  
  67. ' This is the function key handler for converting
  68. ' double-precision real numbers
  69.  
  70. FKey2Handler:
  71.   INPUT "Enter a double-precision floating point number: ", _
  72.          ieeeDouble.Bytes
  73.   doubleCvBuf.Bytes = MKDMBF$(ieeeDouble.Bytes)
  74.   LSET mbfDouble = doubleCvBuf
  75.   PRINT
  76.   PRINT "The number you typed in IEEE format is ";
  77.   CALL ShowDouble(ieeeDouble)
  78.   PRINT "The number you typed in MBF format is ";
  79.   CALL ShowDouble(mbfDouble)
  80.   X# = mbfDouble.Bytes
  81.   PRINT "And check this out: "; ieeeDouble.Bytes, X#
  82.   RETURN
  83.  
  84. ' This is the function key handler to exit the program
  85.  
  86. FKey3Handler:
  87.   Done% = TRUE
  88.   RETURN
  89.  
  90.  
  91. FUNCTION LZPadStr$ (N$, pLen%) STATIC
  92.  
  93. '  Function to pad a string variable
  94. '  on the left with zeroes and return it as a string.
  95.  
  96.   LZPadStr$ = STRING$(pLen% - LEN(N$), "0") + N$
  97. END FUNCTION
  98.  
  99. SUB ShowSingle (A AS TrickSingle) STATIC
  100.  
  101. ' Subprogram to display a single-precision floating-point
  102. ' number in hexadecimal notation
  103.  
  104.   X = VARPTR(A)
  105.   FOR Byte% = 3 TO 0 STEP -1
  106.      PRINT LZPadStr$(HEX$(PEEK(X + Byte%)), 2);
  107.   NEXT Byte%
  108.   PRINT
  109. END SUB
  110.  
  111. SUB ShowDouble (A AS TrickDouble) STATIC
  112.  
  113. '  Subprogram to display a double-precision floating-point
  114. '  number in hexadecimal notation
  115.  
  116.   X = VARPTR(A)
  117.   FOR Byte% = 7 TO 0 STEP -1
  118.      PRINT LZPadStr$(HEX$(PEEK(X + Byte%)), 2);
  119.   NEXT Byte%
  120.   PRINT
  121. END SUB
  122.  
  123.  
  124.